package com.car.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.mapper.ComClassify2imgMapper;
import com.car.model.common.dtos.ResponseResult;
import com.car.model.common.enums.AppHttpCodeEnum;
import com.car.model.component.pojos.ComClassify2_img;
import com.car.service.ComClassify2imgService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ComClassify2imgServiceImpl extends ServiceImpl<ComClassify2imgMapper, ComClassify2_img> implements ComClassify2imgService {
    @Override
    public ResponseResult saveImg(ComClassify2_img classify2Img) {
        if (classify2Img == null || classify2Img.getUser_id() == null || classify2Img.getImg() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (classify2Img.getCreatTime() == null){
            classify2Img.setCreatTime(new Date());
        }
        save(classify2Img);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult getImgByUserId(Integer userId) {
        if (userId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //考虑到用户可能上传了多张照片,但是我们只要用户最后一次上传的,所一我们对时间进行倒序排序拿第一个
        ComClassify2_img one = getOne(Wrappers.<ComClassify2_img>lambdaQuery().eq(ComClassify2_img::getUser_id, userId).orderByDesc(ComClassify2_img::getCreatTime));
        if (one == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"查无图片");
        }
        return ResponseResult.okResult(one);
    }
}
